var waybill_deliverylist = {
    init: function() {
        var that = this;
        that.dateInit();
        jQuery.validator.addMethod("isMobile", function(value, element) {
            var length = value.length;
            var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
            return this.optional(element) || (length == 11 && mobile.test(value));
        }, "填写正确的手机号码");


        $.ajax({
            url: COMMONURL + "/api/bidload/GetTranspotList?token=" + $.cookie('token'),
            type: "post",
            data: {
                consignee_time1: 0,
                consignee_time2: 0,
                page: 1,
                pagesize: 20,
                state: 200
            },
            dataType: "json"
        }).done(function(redata) {
            if (redata.recode == '0000' && redata.mod.Items.length > 0) {
                var compiled = _.template($("#waybillDeliveryListTpl").html());
                var odata = redata.mod.Items;
                // alert(that.format(odata[0].start_bid_time))
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].start_bid_time = that.format(odata[i].start_bid_time);
                    odata[i].end_bid_time = that.format(odata[i].end_bid_time);
                    odata[i].consignee_time = that.format(odata[i].consignee_time);
                }

                $("#waybillStoreListCont").html(compiled(redata.mod));

                $("#wayDeldemo").show().jqPaginator({
                    //totalPages: Math.ceil(redata.mod.TotalNum/20),
                    totalPages: redata.mod.TotalPageCount,
                    visiblePages: 5,
                    currentPage: 1,
                    first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                    prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                    next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                    last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                    page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                    onPageChange: function(num, type) {

                        $("#wdpage").val(num);
                        if (type == 'change') {
                            that.doPage();
                        }
                    }
                });

                //关闭
                $('#closecpickModel').on('show.bs.modal', function(event) {
                    var button = $(event.relatedTarget),
                        bidid = button.data('itemid'),
                        bid_no = button.data('bno'),
                        load_no = button.data('loadno');
                    $(this).attr({
                        'bidid': bidid,
                        'bid_no': bid_no,
                        'load_no': load_no
                    });
                });

                $("#closedeliveryForm").validate({ //关闭提交
                    errorClass: 'error',
                    rules: {
                        closecontacts: 'required',
                        closecontacts_phone: {
                            required: true,
                            minlength: 11,
                            isMobile: true
                        }
                    },
                    messages: {
                        closecontacts: "填写正确的联系人",
                        closecontacts_phone: {
                            required: "填写正确的联系电话",
                            minlength: "请确认号码不能小于11个字符",
                            isMobile: "填写正确的联系电话"
                        }
                    },
                    submitHandler: function(form) {
                        var bidid = $('#closecpickModel').attr('bidid'),
                            bid_no = $('#closecpickModel').attr('bid_no'),
                            load_no = $('#closecpickModel').attr('load_no'),

                            transport_status = $("#closedelTransport option:selected").val(),
                            goods_status = $("#closedelType option:selected").val(),
                            damage_flag = $('input[name="closetoarea"]:checked').val(),
                            damage = ($("#closeexampleInputAmount").val() == null) ? 0 : $("#closeexampleInputAmount").val(),
                            freeze_funds = ($("#ecloseexampleInputAmount").val() == null) ? 0 : $("#ecloseexampleInputAmount").val(),
                            reason_info = $.trim($("#closemessageText").val()),
                            contacts = $("#closecontacts").val(),
                            contacts_phone = $("#closecontacts_phone").val();
                        $.ajax({
                            url: COMMONURL + "api/bidload/ClsoeBid?token=" + $.cookie('token'),
                            type: "post",
                            data: {
                                bid_id: bidid,
                                bid_no: bid_no,
                                load_no: load_no,
                                reason: transport_status,
                                goods_info: goods_status,
                                damage_flag: damage_flag,
                                damage: damage,
                                freeze_funds: freeze_funds,
                                reason_info: reason_info,
                                contacts: contacts,
                                contacts_phone: contacts_phone
                            }
                        }).done(function(redata) {
                            if (redata.recode == '0000') {
                                alert('操作成功');
                                $('#receiptModel').modal('hide');
                                window.location.reload();
                            }
                        });
                    }
                })




                //回单确认
                $('#receiptModel').on('show.bs.modal', function(event) {
                    var button = $(event.relatedTarget),
                        bidid = button.data('itemid'),
                        bid_no = button.data('bno'),
                        load_no = button.data('loadno');
                    $(this).attr({
                        'bidid': bidid,
                        'bid_no': bid_no,
                        'load_no': load_no
                    });
                });

                $("#deliveryForm").validate({
                    errorClass: 'error',
                    rules: {
                        contacts: 'required',
                        contacts_phone: {
                            required: true,
                            minlength: 11,
                            isMobile: true
                        }
                    },
                    messages: {
                        contacts: "填写正确的联系人",
                        contacts_phone: {
                            required: "填写正确的联系电话",
                            minlength: "请确认号码不能小于11个字符",
                            isMobile: "填写正确的联系电话"
                        }
                    },
                    submitHandler: function(form) {
                        var bidid = $('#receiptModel').attr('bidid'),
                            bid_no = $('#receiptModel').attr('bid_no'),
                            load_no = $('#receiptModel').attr('load_no');

                        transport_status = $("#delTransport option:selected").val(),
                            goods_status = $("#delType option:selected").val(),
                            damage_flag = $('input[name="toarea"]:checked').val(),
                            damage = ($.trim($("#exampleInputAmount").val()) == '') ? 0 : $("#exampleInputAmount").val(),
                            reason_info = $.trim($("#messageText").val()),
                            contacts = $("#contacts").val(),
                            contacts_phone = $("#contacts_phone").val();
                        $.ajax({ //回单确认提交
                            url: COMMONURL + "api/bidload/Pickback?token=" + $.cookie('token'),
                            type: "post",
                            data: {
                                bid_id: bidid,
                                bid_no: bid_no,
                                load_no: load_no,
                                transport_status: transport_status,
                                goods_status: goods_status,
                                damage_flag: damage_flag,
                                damage: damage,
                                reason_info: reason_info,
                                contacts: contacts,
                                contacts_phone: contacts_phone
                            }
                        }).done(function(redata) {

                            if (redata.recode == '0000') {
                                alert('操作成功');
                                $('#receiptModel').modal('hide');
                                window.location.reload();
                            }
                        });
                    }
                })
            } else {
                var compiled = _.template($("#waybillDeliveryListTpl").html());

                $("#waybillStoreListCont").html(compiled(redata.mod));
                $("#wayDeldemo").hide();
            }
        });

        $("#HDQUery").click(function() {
            that.doSearch();
        });
    },
    doSearch: function() {
        var that = this;
        var page = $.trim($("#wdpage").val());
        var consignee_time1 = $.trim($("#Hdconsignee_time1").val()) == '' ? 0 : new Date($.trim($("#Hdconsignee_time1").val())).getTime(),
            consignee_time2 = $.trim($("#Hdconsignee_time2").val()) == '' ? 0 : new Date($.trim($("#Hdconsignee_time2").val())).getTime();
        $.ajax({
            url: COMMONURL + "/api/bidload/GetTranspotList?token=" + $.cookie('token'),
            type: "post",
            data: {
                consignee_time1: consignee_time1,
                consignee_time2: consignee_time2,
                page: page,
                pagesize: 20,
                state: 200
            },
            dataType: "json"
        }).done(function(redata) {
            if (redata.recode == '0000' && redata.mod.Items.length > 0) {
                var compiled = _.template($("#waybillDeliveryListTpl").html());
                var odata = redata.mod.Items;
                // alert(that.format(odata[0].start_bid_time))
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].start_bid_time = that.format(odata[i].start_bid_time);
                    odata[i].end_bid_time = that.format(odata[i].end_bid_time);
                    odata[i].consignee_time = that.format(odata[i].consignee_time);
                }

                $("#waybillStoreListCont").html(compiled(redata.mod));
                $("#wayDeldemo").show().jqPaginator({
                    //totalPages: Math.ceil(redata.mod.TotalNum/20),
                    totalPages: redata.mod.TotalPageCount,
                    visiblePages: 5,
                    currentPage: 1,
                    first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                    prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                    next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                    last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                    page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                    onPageChange: function(num, type) {

                        $("#wdpage").val(num);
                        if (type == 'change') {
                            that.doPage();
                        }
                    }
                });

            } else {
                var compiled = _.template($("#waybillDeliveryListTpl").html());
                var odata = redata.mod.Items;
                $("#waybillStoreListCont").html(compiled(redata.mod));
                $("#wayDeldemo").hide();
            }
        });
    },
    doPage: function() {
        var that = this;
        var page = $.trim($("#wdpage").val());
        var consignee_time1 = $.trim($("#Hdconsignee_time1").val()) == '' ? 0 : new Date($.trim($("#Hdconsignee_time1").val())).getTime(),
            consignee_time2 = $.trim($("#Hdconsignee_time2").val()) == '' ? 0 : new Date($.trim($("#Hdconsignee_time2").val())).getTime();
        $.ajax({
            url: COMMONURL + "/api/bidload/GetTranspotList?token=" + $.cookie('token'),
            type: "post",
            data: {
                consignee_time1: consignee_time1,
                consignee_time2: consignee_time2,
                page: page,
                pagesize: 20,
                state: 200
            },
            dataType: "json"
        }).done(function(redata) {
            if (redata.recode == '0000' && redata.mod.Items.length > 0) {
                var compiled = _.template($("#waybillDeliveryListTpl").html());
                var odata = redata.mod.Items;
                // alert(that.format(odata[0].start_bid_time))
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].start_bid_time = that.format(odata[i].start_bid_time);
                    odata[i].end_bid_time = that.format(odata[i].end_bid_time);
                    odata[i].consignee_time = that.format(odata[i].consignee_time);
                }

                $("#waybillStoreListCont").html(compiled(redata.mod));

            } else {
                var compiled = _.template($("#waybillDeliveryListTpl").html());
                var odata = redata.mod.Items;
                $("#waybillStoreListCont").html(compiled(redata.mod));
                $("#wayDeldemo").hide();
            }
        });
    },
    dateInit: function() {
        $('.datepicker').datetimepicker({
            language: 'zh-CN',
            weekStart: 1,
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            format: 'yyyy-mm-dd hh:ii'
        });
    },
    format: function(nS) {
        //return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
        if (!nS) return null;
        var date = new Date(nS * 1000);
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var d = date.getDate();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var seconds = date.getSeconds();
        return year + '-' + month + '-' + d + ' ' + hours + ':' + minutes;
    }
}

waybill_deliverylist.init();
